package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.YddAdvimgDto;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.service.ISysRoleService;
import com.itheima.ydd.service.IYddAdvimgService;
import com.itheima.ydd.service.IYddAdvposService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/ydd_advimg")
public class YddAdvimgController {
    @Autowired
    private IYddAdvimgService yddAdvimgService;
    @Autowired
    private IYddAdvposService yddAdvposService;
    @Autowired
    private ISysRoleService sysRoleService;

    /**
     * 分页查询
     * @param advimgdesc
     * @param advimgpos
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData list(String advimgdesc,String advimgpos ,int page,int limit){
        //YddAdvimg获取数据
        Page<YddAdvimg> pageInfo=new Page<>(page,limit);
        LambdaQueryWrapper<YddAdvimg> lqw=new LambdaQueryWrapper<>();
        lqw.like(advimgdesc!=null,YddAdvimg::getAdvimgdesc,advimgdesc);
        lqw.like(advimgpos!=null,YddAdvimg::getAdvimgpos,advimgpos);
        yddAdvimgService.page(pageInfo, lqw);
        int count = yddAdvimgService.count();

        Page<YddAdvimgDto> dtoPage=new Page<>();
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        List<YddAdvimg> advimgList = pageInfo.getRecords();
        List<YddAdvimgDto> advimgDtos=new ArrayList<>();

        for (YddAdvimg yddAdvimg : advimgList) {
            YddAdvimgDto dto=new YddAdvimgDto();
            //将yddadvimg数据拷贝到dto中
            BeanUtils.copyProperties(yddAdvimg,dto);
            //获取广告名
            //得到yddAdvpos的id
            String advimgpos1 = yddAdvimg.getAdvimgpos();
            YddAdvpos yddAdvpos = yddAdvposService.getById(advimgpos1);
            String advposname = yddAdvpos.getAdvposname();
            dto.setAdvposname(advposname);
            advimgDtos.add(dto);
        }
        dtoPage.setRecords(advimgDtos);
        PageResult pageResult = PageResult.init((long)count, (long)page, (long)limit, advimgDtos);
        return ResultData.ok("",pageResult);
    }

    /**
     * 添加
     * @param yddAdvimg
     * @return
     */
    @PostMapping("add")
    public ResultData add(@RequestBody YddAdvimg yddAdvimg){
        //yddAdvimg.setDefaultData("1");
        yddAdvimgService.save(yddAdvimg);
        return ResultData.ok("","添加成功");
    }

    /**
     * 根据id获取信息
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(int id) {
        YddAdvimg advimg = yddAdvimgService.getById(id);
        YddAdvpos advpos = yddAdvposService.getById(advimg.getAdvimgpos());
        //String advposname = advpos.getAdvposname();
        //将广告位id替换为名字了    然后替换
        advimg.setAdvimgpos(advpos.getAdvposname());

        return ResultData.ok("",advimg );
    }

    /**
     * 修改
     * @param yddAdvimg
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody YddAdvimg yddAdvimg){
        LambdaQueryWrapper<YddAdvpos> lqw=new LambdaQueryWrapper<>();
        lqw.eq(YddAdvpos::getAdvposname,yddAdvimg.getAdvimgpos());
        YddAdvpos yddAdvpos = yddAdvposService.getOne(lqw);
        if (yddAdvpos!=null){
            yddAdvimg.setAdvimgpos(yddAdvpos.getId()+"");
        }
        yddAdvimgService.updateById(yddAdvimg);

        return ResultData.ok("","修改成功");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData del(int id){
        YddAdvimg yddAdvimg = yddAdvimgService.getById(id);
        if ("1".equals(yddAdvimg.getDefaultData())){
            return ResultData.error("删除失败");
        }
        if (sysRoleService.RoleUtils("广告管理","广告图管理","删除")){
            yddAdvimgService.removeById(id);
            return ResultData.ok("","删除成功");
        }
        return ResultData.error("没有权限无法删除");
    }

}
